package deleteface.bank.jqsoft.cn.deleteface.webservice;

import android.support.annotation.NonNull;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Observer;


public class ResultObserver implements Observer<ResponseBody> {
    private RequestManager.onRequestCallBack callBack;
    public ResultObserver( RequestManager.onRequestCallBack callBack){
        this.callBack=callBack;
    }



    @Override
    public void onNext(@NonNull ResponseBody responseBody) {

        try {
            String res = responseBody.string();
            if (res == null) {
                callBack.onError("请求发生未知错误");
                return;
            }

            if (res.contains("{") && res.contains("}")) {
                int startIndex = res.indexOf("{");
                int endIndex = res.lastIndexOf("}") + 1;
                String json = res.substring(startIndex, endIndex);
                callBack.onSuccess(json);
            }
        } catch (IOException e) {
            e.printStackTrace();
            callBack.onError(e.toString());
        }
    }


    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(@NonNull Throwable e) {
        callBack.onError(e.toString());
    }


}